﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateStraightStructureVariableSectionWall.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "StructureDefaultFamilyNames.h"
#include "IUiView.h"
#include "IUiDocument.h"
#include "LineFamilyPositionCurveSideType.h"
#include "StructureInstanceSecondKeys.h"
#include "StructureInstanceUtils.h"
#include "AddStructureDynamicPannelUtil.h"
#include "StructureLinearInstanceCreator.h"
#include "StructureInstanceCmdUtils.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "IModelView.h"
#include "GbmpModelViewTypes.h"
#include "UiCommonDialog.h"
#include "GbmpControlIds.h"
#include "StructureWallInput.h"
#include "IActionInputEventArgs.h"
#include "IActionInputDialog.h"
#include "UiDocumentViewUtils.h"
#include "ActionBase.h"
#include "ActionDrawStraightElementInput.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

#define ACTION_STRAIGHT_STRUCTURE_VARIABLE_SECTION_WALL_INPUT_RADIO_Y_POSITION     L"gmStraightStructureVariableSectionWallRadioYPosition"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateStraightStructureVariableSectionWall)

CmdCreateStraightStructureVariableSectionWall::CmdCreateStraightStructureVariableSectionWall()
    : CmdCreateStraightStructureInstanceBase(ID_CMD_DRAW_VARIABLE_SECTION_STRUCTURE_STRAIGHT_WALL)
{
}

StructureFamilyUiUtils::FamilyCreators CmdCreateStraightStructureVariableSectionWall::GetDefaultFamilyCreators()
{
    StructureFamilyUiUtils::FamilyCreators defaultFamilyCreators
    {
        { GetDefaultFamilyName(), BuiltInStructureFamilies::CreateBuiltInVariableSectionWallFamilyDocument }
    };

    return defaultFamilyCreators;
}

std::wstring CmdCreateStraightStructureVariableSectionWall::GetDefaultFamilyName()
{
    return DEFAULT_VARIABLE_SECTION_WALL_FAMILY_NAME;
}

OwnerPtr<StructureLinearInstanceInput> CmdCreateStraightStructureVariableSectionWall::GetInstanceInput(const std::vector<Vector3d>& pts) const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IUiView* pUIView = UiDocumentViewUtils::GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIView, L"pUIView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureWallInput> wallInput = NEW_AS_OWNER_PTR(StructureWallInput);

    double thickness(0);
    bool bOk = StructureInstanceUtils::GetWallCoreThickness(pDoc, m_instanceTypeId, thickness);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"GetBeamThickness失败",L"GDMPLab",L"2024-03-30");

    DBG_WARN_AND_RETURN_FALSE_UNLESS(!pts.empty(), L"points无效",L"GDMPLab",L"2024-03-30");
    std::vector<Vector3d> positioningPoints = StructureInstanceUtils::GetPositioningPoints(pts, thickness, m_yPositioningType);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(!positioningPoints.empty(), L"GetPositioningPoints失败",L"GDMPLab",L"2024-03-30");
    wallInput->PositionPoints = positioningPoints;

    bOk = StructureInstanceCmdUtils::GenerateStraightVariableSectionWallInput(pDoc, pUIView->GetModelView(),
        this->GetCategory(), this->GetCategorySecondKey(), m_familyId, m_instanceTypeId, *wallInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"GenerateStraightVariableSectionBeamInput失败",L"GDMPLab",L"2024-03-30");

    return TransferOwnership(wallInput);
}

OwnerPtr<IStructureLinearInstanceCreator> CmdCreateStraightStructureVariableSectionWall::GetInstanceCreator() const
{
    return NEW_AS_OWNER_PTR(StructureStraightVariableSectionWallCreator);
}

Int32 CmdCreateStraightStructureVariableSectionWall::GetCategorySecondKey() const
{
    return (Int32)StructureWallType::VariableSection;
}

void CmdCreateStraightStructureVariableSectionWall::SetupRibbonContextualPanel()
{
    AddStructureDynamicPannelUtil::SetupWallCreationContext(m_pAction, ID_PAGE_GBMP_MODIFY, true);
}

gcmp::UniIdentity gcmp::CmdCreateStraightStructureVariableSectionWall::GetCategory() const
{
    UniIdentity categoryUid = BuiltInCategoryUniIdentities::BICU_STRUCTURE_WALL;
    return categoryUid;
}

bool gcmp::CmdCreateStraightStructureVariableSectionWall::OnExecuteCommand() const
{
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, false,L"GDMPLab",L"2024-03-30");
    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"绘制墙"), GBMP_TR(L"此视图下不支持绘制墙"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }
    return true;
}

void gcmp::CmdCreateStraightStructureVariableSectionWall::DefineInputDialog(DefineInputDialogParams& inputItems)
{
    // 对话框标题
    inputItems.title = GBMP_TR(L"结构墙");
    // 定义item选项, 默认选中 “中心”
    m_yPositioningType = LineFamilyPositionCurveSideType::CenterLineFamilyPositionCurveSideType;
    StructureInstanceCmdUtils::AddYPositionInputItemsToDialogNew(ACTION_STRAIGHT_STRUCTURE_VARIABLE_SECTION_WALL_INPUT_RADIO_Y_POSITION,
        m_yPositioningType, inputItems.inputItems);
}

void gcmp::CmdCreateStraightStructureVariableSectionWall::OnActionInputEvent(IActionInputEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");
    const std::pair<std::wstring, gcmp::Any>& msg = pArgs->GetInputMessage();
    const std::wstring& eventID = msg.first;
    gcmp::Any inputValue = msg.second;
    if (eventID == ACTION_STRAIGHT_STRUCTURE_VARIABLE_SECTION_WALL_INPUT_RADIO_Y_POSITION)
    {
        StructureInstanceCmdUtils::GetYPositionFromInputEventNew(inputValue.cast<std::wstring>(), m_yPositioningType);
    }
}
